//
//  ZGLSubDetCollectionViewCell.m
//  Faker
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLSubDetCollectionViewCell.h"
#import "ZGLDetCollectionViewCell.h"
#import "ZGLTitleCollectionViewCell.h"
#import "ZGLImageCollectionViewCell.h"
#import "ZGLPageCollectionViewCell.h"
#import "ZGLSingleDetCollectionViewCell.h"
#import "ZGLSubDetModel.h"

@interface ZGLSubDetCollectionViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *detCollectionView;



@end

@implementation ZGLSubDetCollectionViewCell

- (void)dealloc {
    _detCollectionView.delegate = nil;
    _detCollectionView.dataSource = nil;
}


- (void)createCollectionView {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = 0;
    //flow.itemSize = CGSizeMake(ScaleX * 375 / 2, ScaleY * 100);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    self.detCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScaleX * 375, ScaleY * 647) collectionViewLayout:flow];
    [self.contentView addSubview:self.detCollectionView];
    self.detCollectionView.delegate = self;
    self.detCollectionView.dataSource = self;
    self.detCollectionView.pagingEnabled = YES;
    [self.detCollectionView registerClass:[ZGLDetCollectionViewCell class]
            forCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLDetCollectionViewCelll"];
    [self.detCollectionView registerClass:[ZGLDetCollectionViewCell class]
               forCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLDetCollectionViewCell"];
    [self.detCollectionView registerClass:[ZGLTitleCollectionViewCell class] forCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLTitleCollectionViewCell"];
    [self.detCollectionView registerClass:[ZGLImageCollectionViewCell class] forCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLImageCollectionViewCell"];
    [self.detCollectionView registerClass:[ZGLPageCollectionViewCell class] forCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLPageCollectionViewCell"];
    [self.detCollectionView registerClass:[ZGLSingleDetCollectionViewCell class] forCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLSingleDetCollectionViewCell"];
}

- (void)setDetListArr:(NSMutableArray *)detListArr {
    if (_detListArr != detListArr) {
        _detListArr = detListArr;
    }
    for (NSInteger i = 0; i < detListArr.count; i++) {
        ZGLSubDetModel *model = detListArr[i];
        if (model.media.count > 0) {
            [_detListArr exchangeObjectAtIndex:0 withObjectAtIndex:i];
            break;
        }
    }
   
    [self.detCollectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
        [self createCollectionView];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.backgroundColor = backColor;
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 6;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (2 == section || 3 == section) {
        return 2;
    } else {
        return 1;
    }
}

- (void)setCurrentPage:(NSInteger)currentPage {
    _currentPage = currentPage;
    [self.detCollectionView reloadData];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ZGLTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLTitleCollectionViewCell" forIndexPath:indexPath];
        cell.titleUrl = _titleUrl;
        return cell;
    }
    if (2 == indexPath.section) {
        ZGLDetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLDetCollectionViewCell" forIndexPath:indexPath];
        switch (indexPath.item) {
            case 0:
                cell.subDetModel = _detListArr[1];
                break;
            case 1:
                cell.subDetModel = _detListArr[2];
                break;
        }
        return cell;
    }
    if (3 == indexPath.section) {
        ZGLDetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLDetCollectionViewCelll" forIndexPath:indexPath];
         switch (indexPath.item) {
            case 0:
                cell.subDetModel = _detListArr[3];
                break;
            case 1:
                cell.subDetModel = _detListArr[4];
                break;
        }
        return cell;
    }
    if (1 == indexPath.section) {
        ZGLImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLImageCollectionViewCell" forIndexPath:indexPath];
        cell.subDetModel = _detListArr[0];
        return cell;
    }
    if (4 == indexPath.section) {
        ZGLSingleDetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLSingleDetCollectionViewCell" forIndexPath:indexPath];
        cell.subDetModel = _detListArr[5];
        return cell;
    }
    else {
        ZGLPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLSubDetCollectionViewCell_ZGLPageCollectionViewCell" forIndexPath:indexPath];
        if (_allPage) {
            cell.allPage = _allPage;
            cell.currentPage = _currentPage;
        }
        return cell;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return CGSizeMake(ScaleX * 375, ScaleY * 60);
        
    }else if (indexPath.section == 1){
        
        return CGSizeMake(ScaleX * 375, ScaleY * 240);
        
    }else if (indexPath.section == 2 || indexPath.section == 3){
        
        return CGSizeMake(ScaleX * 187.5, ScaleY * 100);
    } else if (indexPath.section == 4) {
        
        return CGSizeMake(ScaleX * 375,ScaleY * 100);
        
    } else {
        return CGSizeMake(ScaleX * 375,ScaleY * 47);
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZGLSubDetModel *model = nil;
    if (1 == indexPath.section) {
        model = _detListArr[0];
    }
    if (2 == indexPath.section) {
        if (indexPath.row == 0) {
            model = _detListArr[1];
        } else {
            model = _detListArr[2];
        }

    }
    if (3 == indexPath.section) {
        if (indexPath.row == 0) {
            model = _detListArr[3];
        } else {
            model = _detListArr[4];
        }
    }
    if (4 == indexPath.section) {
        model = _detListArr[5];
    }
    [self.delegate pass:model];
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 5) {
        return NO;
    } else {
        return YES;
    }
}

@end
